/*
 * 文件名：CustAnalysisDaoImp.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2013-2-21
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.customer.custanalysis.dao.imp;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wootide.cms.common.Page;
import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.common.util.CommonUtil;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.customer.custanalysis.dao.CustAnalysisDao;
import com.wootide.cms.service.customer.custmanager.pojo.Customer;
import com.wootide.cms.service.facility.room.roominfo.pojo.RoomInfo;
import com.wootide.cms.service.facility.room.roominfo.pojo.RoomPeriod;
import com.wootide.cms.service.reservation.bookinfo.pojo.BookOrderInfo;

public class CustAnalysisDaoImp extends HibernateDaoSupport implements
        CustAnalysisDao
{
    // 注入运行日志
    private RunLog runLog;
    
    public int findNumBookOrderInfo(Integer customerId)
    {
        runLog.debug("customerId=" + customerId);
        String sql = "select count(a) from BookOrderInfo a where a.customerInfo ="
                + customerId;
        return ((Number)getHibernateTemplate().find(sql).get(0)).intValue();
    }
    
    @SuppressWarnings("unchecked")
    public Customer findCustomerInfoByCustomerId(Integer value)
    {
        String sql = "from Customer a where a.customerId =" + value;
        List<Customer> list = getHibernateTemplate().find(sql);
        return list.isEmpty() ? null : list.get(0);
    }
    
    @SuppressWarnings("unchecked")
    public List<BookOrderInfo> findBookOrderInfoLastTimeByCustomerId(
            Integer customerId)
    {
        String sql = "from BookOrderInfo a where a.customerInfo =" + customerId
                + " order by deneratedDate desc";
        List<BookOrderInfo> list = getHibernateTemplate().find(sql);
        return list.isEmpty() ? null : list;
    }
    
    @SuppressWarnings("unchecked")
    public RoomPeriod findRoomPeriodByOrderNum(String OrderNum)
    {
        String sql = "from RoomPeriod a where a.bookOrderInfo =" + OrderNum;
        List<RoomPeriod> list = getHibernateTemplate().find(sql);
        return list.isEmpty() ? null : list.get(0);
    }
    
    @SuppressWarnings("unchecked")
    public List<RoomInfo> findAllRoomByCustomerId(Integer customerId)
    {
        String sql = "from RoomInfo a where a.innerIndex in (select r.roomInfo.innerIndex from RoomPeriod r where r.bookOrderInfo.orderNum in (select b.orderNum from BookOrderInfo b where b.customerInfo.customerId = "+customerId+")) group by a.roomNum";
        List<RoomInfo> list = getHibernateTemplate().find(sql);
        return list.isEmpty() ? null : list;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public QueryResults queryForPages(final Page page,
            final QueryCondition queryConditon)
    {
        // 打印方法入口参数
        runLog.debug("page=" + page);
        runLog.debug("queryConditon" + queryConditon);
        
        List queryResults = (List)getHibernateTemplate().execute(new HibernateCallback()
        {
            public Object doInHibernate(Session session) throws SQLException
            {
                //                        Criteria criteria = session
                //                                .createCriteria(queryConditon.getObjClass());
                Criteria criteria = session.createCriteria(queryConditon.getObjClass());
                // 如果查询条件不为空
                if (CommonUtil.conditionExist(queryConditon))
                {
                    String conditionKey = queryConditon.getKey();
                    String strCondition = "%" + queryConditon.getValue().trim()
                            + "%";
                    if (conditionKey.equals("customerId"))
                    {
                        criteria.add(Restrictions.sqlRestriction("CAST({alias}."
                                + conditionKey + " AS CHAR) like ?",
                                strCondition,
                                Hibernate.STRING));
                    }
                    else
                    {
                        criteria.add(Restrictions.ilike(conditionKey,
                                strCondition));
                    }
                }
                // 获得总记录数
                int totalNum = ((Number)criteria.setProjection(Projections.rowCount())
                        .list()
                        .get(0)).intValue();
                page.setTotalNum(totalNum);
                
                criteria.setProjection(null);
                // 获得分页查询数据
                List results = criteria.setMaxResults(page.getNumPerPage())
                        .setFirstResult(page.getFirstResult())
                        .list();
                return results;
            }
        });
        QueryResults result = new QueryResults();
        result.setResults(queryResults);
        result.setPage(page);
        return result;
    }
    
    public RunLog getRunLog()
    {
        return runLog;
    }
    
    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }
    
}
